package com.example.ice.fittime.adapter.user;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.ice.fittime.R;
import com.example.ice.fittime.entry.user.ActivityUserBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ice on 2016/10/13.
 */

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.VH> {

    private Context context;
    private List<ActivityUserBean> allData;

    public PhotoAdapter(Context context) {
        this.context = context;
        allData = new ArrayList<>();
    }

    public void addAll(List<ActivityUserBean> newData) {
        int oldSize = allData.size();
        allData.addAll(newData);
        notifyItemRangeInserted(oldSize, newData.size());
    }

    public void removeAll() {
        int oldSize = allData.size();
        allData.clear();
        notifyItemRangeRemoved(0, oldSize);
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setAdjustViewBounds(true);
        return new PhotoAdapter.VH(imageView);
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        Glide.with(context).load(allData.get(position).getImage().getUrl()).placeholder(R.mipmap.no_pic).into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return allData == null ? 0 : allData.size();
    }

    public class VH extends RecyclerView.ViewHolder {

        private ImageView imageView;

        public VH(ImageView imageView) {
            super(imageView);
            this.imageView = imageView;
        }
    }
}
